package mcchristian;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class Pic_Function {
	
	public List<Albums_Folder> getAlbums_Folder(String filePath) {
		List<Albums_Folder> albums_folders = new ArrayList<Albums_Folder>();
		String folder_name;
		String pic_path;
	    File folder = new File(filePath);
	    File[] listOfFiles = folder.listFiles(); 
		for (int i = 0; i < listOfFiles.length; i++) 
		{
			folder_name = listOfFiles[i].getName();
			pic_path = getCover_Pic(filePath + "/" + folder_name, folder_name);
			albums_folders.add(new Albums_Folder(folder_name, "albums/" + folder_name, pic_path));
		}
		return albums_folders;
	}
	
	public List<Albums> getAlbums_Pic(String filePath, String albums) {
		List<Albums> albumses = new ArrayList<Albums>();
		String folder_name;
	    File folder = new File(filePath);
	    File[] listOfFiles = folder.listFiles(); 
		for (int i = 0; i < listOfFiles.length; i++) 
		{
			folder_name = listOfFiles[i].getName();
			String[] folder_names = folder_name.split("\\.");
			if(folder_names.length > 0){
				if(folder_names[folder_names.length-1].equals("jpg") & !folder_name.equals("cover.jpg")){
					albumses.add(new Albums(folder_names[0], "albums/" + albums + "/" + folder_name));
				}
			}
		}
		return albumses;
	}
	
	public String getCover_Pic(String folderPath, String folderName){
		String cover_pic = "";
		String cover_pic_folder = "";
		String pic_name = "";
		File folder = new File(folderPath);
		File[] listOfFiles = folder.listFiles();
		if (listOfFiles.length > 0){
			pic_name = listOfFiles[0].getName();
			cover_pic_folder = folderPath + "/cover.jpg";
			try{
				if((new File(cover_pic_folder)).exists()){
					cover_pic = "albums/" + folderName + "/cover.jpg";
				}else{
					cover_pic = "albums/" + folderName + "/" + pic_name;
				}
			}catch(Exception e){
				cover_pic = "albums/" + folderName + "/" + pic_name;
			}		
		}else{
			cover_pic = "img/" + "cover.gif";
		}
		return cover_pic;
	}
	
    public String getType(String srcImageFile){  
    	try{  
    		BufferedImage src = ImageIO.read(new File(srcImageFile)); 
	        int width = src.getWidth(); 
	        int height = src.getHeight();
	        if (height > width) {
	        	return "height";
	        }  
	        else {
	        	return "width";
	        }  
	   }catch (Exception e){  
		   e.printStackTrace(); 
		   return "width";
	   }
    }  
	
	 public static void scale(String srcImageFile, String result, int scale, boolean flag){  
	        try{  
	            BufferedImage src = ImageIO.read(new File(srcImageFile)); 
	            int width = src.getWidth(); 
	            int height = src.getHeight();
	            if (flag) {  
	                width = width * scale;  
	                height = height * scale;  
	            }  
	            else {  
	                width = width / scale;  
	                height = height / scale;  
	            }  
	            Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);  
	            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
	            Graphics g = tag.getGraphics();  
	            g.drawImage(image, 0, 0, null); 
	            g.dispose();  
	            ImageIO.write(tag, "JPEG", new File(result));
	        }  
	        catch (Exception e){  
	            e.printStackTrace();  
	        }  
	    }  
	 
	 
}
